package com.arrcen.medicen.core.match.ctrl;


import com.arrcen.medicen.core.match.model.dto.DtoPage;
import com.arrcen.medicen.core.match.model.dto.DtoSyncTable;
import com.arrcen.medicen.core.match.model.dto.Result;
import com.arrcen.medicen.core.match.model.dto.ResultObj;
import com.arrcen.medicen.core.match.model.entity.SyncTableEntity;
import com.arrcen.medicen.core.match.retention.UseToken;
import com.arrcen.medicen.core.match.service.SyncTable;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("Api")
public class ApiController {

    @Autowired
    SyncTable syncTable;

    @RequestMapping(value = "saveSyncConfig", method = RequestMethod.POST)
    @ApiOperation(value = "保存配置")
    @UseToken
    public Result saveSyncConfig(@RequestBody DtoSyncTable inEnt) {
        return syncTable.saveSyncConfig(inEnt);
    }

    @RequestMapping(value = "getTargetTable", method = RequestMethod.GET)
    @ApiOperation(value = "获取目标表列表")
    @UseToken
    public ResultObj<List<String>> getTargetTable() {
        ResultObj<List<String>> listResultObj = new ResultObj<>();
        listResultObj.setData(syncTable.getTargetTable());
        listResultObj.setStatus(1);
        return listResultObj;
    }

    @RequestMapping(value = "getSourceTable", method = RequestMethod.GET)
    @ApiOperation(value = "获取源表列表")
    @UseToken
    public ResultObj<List<String>> getSourceTable() {
        ResultObj<List<String>> listResultObj = new ResultObj<>();
        listResultObj.setData(syncTable.getSourceTable());
        listResultObj.setStatus(1);
        return listResultObj;
    }

    @RequestMapping(value = "getSourceField", method = RequestMethod.GET)
    @ApiOperation(value = "获取源表字段列表")
    @UseToken
    public ResultObj<List<String>> getSourceField(String tableName) {
        ResultObj<List<String>> listResultObj = new ResultObj<>();
        listResultObj.setData(syncTable.getSourceField(tableName));
        listResultObj.setStatus(1);
        return listResultObj;
    }

    @RequestMapping(value = "getTargetField", method = RequestMethod.GET)
    @ApiOperation(value = "获取目标表字段列表")
    @UseToken
    public ResultObj<List<String>> getTargetField(String tableName) {
        ResultObj<List<String>> listResultObj = new ResultObj<>();
        listResultObj.setData(syncTable.getTargetField(tableName));
        listResultObj.setStatus(1);
        return listResultObj;
    }

    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除配置")
    @UseToken
    public Result delete(int key) {
        return syncTable.delete(key);
    }

    @RequestMapping(value = "updateStatus", method = RequestMethod.PUT)
    @ApiOperation(value = "更新状态1表示正常，0表示禁用")
    @UseToken
    public Result updateStatus(int key,int status) {
        return syncTable.updateStatus(key,status);
    }

    @RequestMapping(value = "getAll", method = RequestMethod.POST)
    @ApiOperation(value = "获取目标表字段列表")
    @UseToken
    public ResultObj<IPage<SyncTableEntity>> getAll(@RequestBody DtoPage dtoPage) {
        ResultObj<IPage<SyncTableEntity>> listResultObj = new ResultObj<>();
        listResultObj.setData(syncTable.getAll(dtoPage));
        listResultObj.setStatus(1);
        return listResultObj;
    }


    @RequestMapping(value = "getDetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取配置详情")
    @UseToken
    public ResultObj<DtoSyncTable> getDetail(int key) {
        ResultObj<DtoSyncTable> listResultObj = new ResultObj<>();
        listResultObj.setData(syncTable.getDetail(key));
        listResultObj.setStatus(1);
        return listResultObj;
    }

}
